<template>
	<!-- 电子标签 -->
	<view class="e_content">
		<br />
		<view v-if="labels.length > 0">
			<fui-section title="今日巡检情况"></fui-section>
			<br />
			<view v-for="(item,index) in labels" :key="index">

				<fui-card v-if="item.serialNumber" @click="nvcatTab" @longpress="onOps(item,index)" showBorder
					width="30" height="30" src="static/tag.png" :title="item.serialNumber + '号标签'"
					:tag="'时段: ' + item.inspectionPeriod">
					<view class="fui-card__content">
						<text>
							标签部位：{{item.position}}
						</text>

						<text v-if="onTime(item.writeTime, time)">
							<fui-icon color="#09BE4F" name="checkbox" :size="30"></fui-icon>已完成
						</text>

						<text v-else>
							<fui-icon color="#FF2B2B" name="clear" :size="30"></fui-icon>未完成
						</text>

					</view>
				</fui-card>

				<br />
			</view>
		</view>

		<view v-if="!labels.length"
			style="width: 100vw; height: 100vh;  display:flex; justify-content: center; align-items: center;">
			<view>
				<fui-empty src="/static/msg/img_data_3x.png" title="数据为空,请点击右上角添加电子标签"></fui-empty>
			</view>
		</view>

	</view>
	<view class="module">
		<view>
			<up-modal @confirm="submittedTab" @cancel="show=!show" :show="show" :title="title" showCancelButton>
				<view class="slot-content">
					<fui-input label="编号:" @click="showNum = !showNum" v-model="labelObj.serialNumber" readonly
						placeholder="请选择"></fui-input>
					<fui-input label="部位:" :maxlength="7" v-model="labelObj.position"
						placeholder="请输入(1-7个字符)"></fui-input>

					<fui-input label="巡检时段:" @click="showTime = !showTime" v-model="labelObj.inspectionPeriod" readonly
						borderTop placeholder="请选择"></fui-input>


					<fui-input label="水印图片:" readonly v-model="labelObj.watermarkImgState.text">
						<template v-slot:default>
							<view class="fui-left__icon">
								<up-switch v-model="labelObj.watermarkImgState.checked" asyncChange
									@change="onWatermarkImg"></up-switch>

							</view>
						</template>
					</fui-input>

					<fui-input label="精确定位:" readonly v-model="labelObj.preciseLocation.text">
						<template v-slot:default>
							<view class="fui-left__icon">
								<up-switch v-model="labelObj.preciseLocation.checked" asyncChange
									@change="onPreciseLocation"></up-switch>
							</view>
						</template>
					</fui-input>

					<fui-input label="详细地址" readonly borderTop placeholder="请选择地址" v-model="labelObj.address">
						<template v-slot:default>
							<view class="fui-left__icon">

								<fui-icon name="location" @click="selctAddress" color="#333" :size="48"></fui-icon>
							</view>
						</template>
					</fui-input>
				</view>
			</up-modal>
		</view>

		<view>
			<up-modal title="编辑" showCancelButton @cancel="closeItemInfo" :show="showItemInfo" :asyncClose="true"
				closeOnClickOverlay @close="showItemInfo=!showItemInfo" @open="open" @confirm="onTabUpData">
				<view>
					<fui-input label="编号:" v-model="labelObj.serialNumber" readonly placeholder="请选择"></fui-input>
					<fui-input label="部位:" :maxlength="7" v-model="labelObj.position"
						placeholder="请输入(1-7个字符)"></fui-input>

					<fui-input label="巡检时段:" @click="showTime = !showTime" v-model="labelObj.inspectionPeriod" readonly
						borderTop placeholder="请选择"></fui-input>


					<fui-input label="水印图片:" readonly v-model="labelObj.watermarkImgState.text">
						<template v-slot:default>
							<view class="fui-left__icon">
								<up-switch v-model="labelObj.watermarkImgState.checked" asyncChange
									@change="onWatermarkImg"></up-switch>

							</view>
						</template>
					</fui-input>

					<fui-input label="精确定位:" readonly v-model="labelObj.preciseLocation.text">
						<template v-slot:default>
							<view class="fui-left__icon">
								<up-switch v-model="labelObj.preciseLocation.checked" asyncChange
									@change="onPreciseLocation"></up-switch>
							</view>
						</template>
					</fui-input>

					<fui-input label="详细地址" readonly borderTop placeholder="请选择地址" v-model="labelObj.address">
						<template v-slot:default>
							<view class="fui-left__icon">
								<fui-icon name="location" @click="selctAddress" color="#333" :size="48"></fui-icon>
							</view>
						</template>
					</fui-input>
				</view>

			</up-modal>
		</view>

		<view>
			<up-picker title="选择编号" @change="onVibrate" :show="showNum" @cancel="showNum = !showNum" @confirm="oOpsNum"
				:columns="columnsNum"></up-picker>
		</view>

		<view>
			<up-picker title="选择时段" @change="onVibrate" :show="showTime" @cancel="showTime = !showTime"
				@confirm="oOpsTime" :columns="columnsTime"></up-picker>
		</view>

		<view>
			<up-picker title="选择时段" @change="onVibrate" :show="showUpTime" @cancel="showUpTime = !showUpTime"
				@confirm="onUpTime" :columns="columnsTime"></up-picker>
		</view>

		<view>
			<up-action-sheet round="18" title="更多" :actions="opsList" @select="onChoice" @close="showOps =!showOps"
				:show="showOps"></up-action-sheet>
		</view>
	</view>
</template>

<script>
	const KJNFC = ""
	
	import moment from "moment"
	import {
		BASE_IP
	} from '@/common/http/url.js'
	import nfc from "../../common/nfc"
	export default {
		data() {
			return {
				showItemInfo: false,
				show: false,
				title: '标签录入',
				columnsNum: [
					[
						1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
						27, 28, 29, 30
					]
				],
				labelObj: {
					id: 0,
					serialNumber: "", //编号
					position: "", //部位
					inspectionPeriod: "", //时段
					address: "", //详细地址
					latitude: "", //纬度
					longitude: "", //经度
					watermarkImgState: {
						text: "关闭",
						state: 0,
						checked: false
					},
					preciseLocation: {
						text: "关闭",
						state: 0,
						checked: false
					}
				},
				showNum: false,
				showTime: false,
				showUpTime: false,
				columnsTime: [
					[
						"08:00 - 10:00", "10:00 - 12:00", "12:00 - 14:00", "14:00 - 16:00", "16:00 - 18:00",
						"18:00 - 20:00",
						"20:00 - 22:00", "22:00 - 00:00", "00:00 - 02:00", "02:00 - 04:00", "04:00 - 06:00",
						"06:00 - 08:00"
					]
				],
				labels: [],
				showOps: false,
				opsList: [{
						id: 6,
						name: "编辑",
						color: '#409EFF',
					},
					{
						id: 5,
						name: "修改时段"
					},
					{
						id: 4,
						name: "修改部位"
					},
					{
						id: 1,
						name: "删除",
						color: '#ff0000',
					},
					{
						id: 2,
						name: "全部删除"
					}
				],
				item: "",
				index: "",
				time: "",
				recorditme: ""
			}
		},
		methods: {
			onTabUpData() {
				if (this.labelObj.serialNumber == "") {
					uni.showToast({
						title: "请输入编号",
						icon: "error"
					})
					return
				}
				if (this.labelObj.position == "") {
					uni.showToast({
						title: "请输入部位",
						icon: "error"
					})
					return
				}
				if (this.labelObj.inspectionPeriod == "") {
					uni.showToast({
						title: "请输入时段",
						icon: "error"
					})
					return
				}
				if(this.labelObj.address == ""){
					uni.showToast({
						title: "请输入地址",
						icon: "error"
					})
					return
				}
				
				let user = uni.getStorageSync("user")
				let url = BASE_IP + "pc/tab/update/" + user.unitId
				uni.request({
					header: {
						"Authorization": user.token,
						"Content-Type": "application/json"
					},
					url: url,
					method: "POST",
					dataType: "json",
					data: {
						"ElectronicTabJson": this.labelObj
					},
					success: (res) => {
						console.log(res)
						if (res.statusCode == 200) {
							if (res.data.code == 1) {
								uni.showToast({
									title: res.data.data,
									icon: "success"
								})
								this.queryTab()
								this.closeItemInfo()
							} else {
								uni.showToast({
									title: res.data.msg,
									icon: "error"
								})
							}
						} else {
							uni.showToast({
								title: "服务器出现问题",
								icon: "error"
							})
						}
					}
				})
			},
			closeItemInfo() {
				this.labelObj.address = ""
				this.labelObj.inspectionPeriod = ""
				this.labelObj.latitude = ""
				this.labelObj.longitude = ""
				this.labelObj.position = ""
				this.labelObj.preciseLocation.checked = false
				this.labelObj.preciseLocation.state = 0
				this.labelObj.preciseLocation.text = "关闭"

				this.labelObj.watermarkImgState.checked = false
				this.labelObj.watermarkImgState.state = 0
				this.labelObj.watermarkImgState.text = "关闭"
				this.labelObj.serialNumber = ""
				this.showItemInfo = !this.showItemInfo
			},
			onShowItemInfo(res) {
				this.item = res
				console.log(res)
				this.showItemInfo = !this.showItemInfo
			},
			onPreciseLocation(res) {
				console.log(res)
				if (res) {
					uni.showModal({
						title: "提示",
						content: "开启精确定位，在使用此标签巡检时必须在记录地点10米范围之内",
						success: (res) => {
							if (res.confirm) {
								this.labelObj.preciseLocation.text = "开启"
								this.labelObj.preciseLocation.state = 1
								this.labelObj.preciseLocation.checked = true
							}
							if (res.cancel) {
								this.labelObj.preciseLocation.text = "关闭"
								this.labelObj.preciseLocation.state = 0
								this.labelObj.preciseLocation.checked = false
								return
							}
						}
					})
				} else {
					this.labelObj.preciseLocation.text = "关闭"
					this.labelObj.preciseLocation.state = 0
					this.labelObj.preciseLocation.checked = false
				}
			},
			onWatermarkImg(res) {
				console.log(res)
				if (res) {
					uni.showModal({
						title: "提示",
						content: "开启水印图片巡检时会强制上传水印巡检图片",
						success: (res) => {
							if (res.confirm) {
								this.labelObj.watermarkImgState.text = "开启"
								this.labelObj.watermarkImgState.state = 1
								this.labelObj.watermarkImgState.checked = true
							}
							if (res.cancel) {
								this.labelObj.watermarkImgState.text = "关闭"
								this.labelObj.watermarkImgState.state = 0
								this.labelObj.watermarkImgState.checked = false
								return
							}
						}
					})
				} else {
					this.labelObj.watermarkImgState.text = "关闭"
					this.labelObj.watermarkImgState.state = 0
					this.labelObj.watermarkImgState.checked = false
				}
			},
			selctAddress() {
				uni.chooseLocation({
					success: (res) => {
						console.log(res)
						console.log('位置名称：' + res.name);
						console.log('详细地址：' + res.address);
						this.labelObj.address = res.address
						this.labelObj.latitude = res.latitude
						this.labelObj.longitude = res.longitude

						console.log('纬度：' + res.latitude);
						console.log('经度：' + res.longitude);
					}
				});

			},
			onTime(v1, v2) {
				if (v1 == v2) {
					return true
				} else {
					return false
				}
				return true
			},
			nvcatTab() {
				uni.navigateTo({
					url: "/pagesWork/records/record_details?data=" + this.recorditme
				})
			},
			onChoice(res) {
				console.log(res)
				if (res.id == 6) {
					this.labelObj.serialNumber = this.item.serialNumber
					this.labelObj.position = this.item.position
					this.labelObj.inspectionPeriod = this.item.inspectionPeriod
					this.labelObj.address = this.item.address
					this.labelObj.latitude = this.item.latitude
					this.labelObj.longitude = this.item.longitude
					if (this.item.watermarkImgState == 1) {
						this.labelObj.watermarkImgState.checked = true
						this.labelObj.watermarkImgState.state = 1
						this.labelObj.watermarkImgState.text = "开启"
					} else {
						this.labelObj.watermarkImgState.checked = false
						this.labelObj.watermarkImgState.state = 0
						this.labelObj.watermarkImgState.text = "关闭"
					}
					if (this.item.preciseLocation == 1) {
						this.labelObj.preciseLocation.checked = true
						this.labelObj.preciseLocation.state = 1
						this.labelObj.preciseLocation.text = "开启"
					} else {
						this.labelObj.preciseLocation.checked = false
						this.labelObj.preciseLocation.state = 0
						this.labelObj.preciseLocation.text = "关闭"
					}

					console.log("labelObj", this.labelObj)
					this.showItemInfo = !this.showItemInfo
				}
				if (res.id == 5) {
					console.log(555)
					this.showUpTime = !this.showUpTime
				}
				if (res.id == 4) {
					let labels = this.labels
					let item = this.item
					uni.showModal({
						title: "修改部位",
						placeholderText: "请输入部位",
						editable: true,
						success: (res) => {
							if (res.confirm) {
								if (!res.content) {
									uni.showToast({
										title: "未输入",
										icon: "error"
									})
									return
								}
								for (let index in labels) {
									if (labels[index].id == item.id) {
										labels[index].position = res.content
										this.labels = labels
										this.tabUpData(item.id, res.content, 4)
										uni.setStorageSync("labels", labels)
									}
								}

							}
						}
					})
					console.log(item)
				}
				if (res.id == 1) {
					uni.showModal({
						title: "提示",
						content: "您确定要删除此标签吗？\n删除后此标签无法用于巡检",
						success: (res) => {
							if (res.confirm) {
								let l = this.labels
								l.splice(this.index, 1)
								this.labels = l
								uni.setStorageSync("labels", l)
								this.tabDel()
							}
						}
					})
				}
				if (res.id == 2) {
					uni.showModal({
						title: "提示",
						content: "您确定要删除全部标签吗\n删除后标签无法用于巡查",
						success: (res) => {
							if (res.confirm) {
								this.labels = []
								uni.setStorageSync("labels", this.labels)
								this.tabDelAll()
							}
						}
					})
				}
			},
			onUpTime(res) {
				console.log(res)
				let labels = this.labels
				let item = this.item
				let inspectionPeriod = res.value[0]
				for (let index in labels) {
					if (labels[index].id == item.id) {
						labels[index].inspectionPeriod = inspectionPeriod
						this.labels = labels
						this.tabUpData(item.id, inspectionPeriod, 5)
						uni.setStorageSync("labels", labels)
					}
				}
				this.showUpTime = !this.showUpTime

			},
			tabUpData(id, data, mid) {
				let url = BASE_IP + "pc/tab/up/" + id + "/" + data + "/" + mid
				let user = uni.getStorageSync("user")
				uni.request({
					header: {
						"Authorization": user.token,
						'Content-Type': 'application/x-www-form-urlencoded'
					},
					url: url,
					method: "POST",
					success: (res) => {
						// console.log(res)
						if (res.statusCode == 200) {
							uni.showToast({
								title: "更新成功",
								icon: "success"
							})
						}
					}
				})
			},
			tabDelAll() {
				let user = uni.getStorageSync("user")
				let url = BASE_IP + "pc/tab/del/all/" + user.unitId
				uni.request({
					header: {
						"Authorization": user.token,
						'Content-Type': 'application/x-www-form-urlencoded'
					},
					url: url,
					method: "DELETE",
					success: (res) => {
						console.log(res)
					}
				})
			},
			tabDel() {
				let user = uni.getStorageSync("user")
				let url = BASE_IP + "pc/tab/del/" + this.item.id
				uni.request({
					header: {
						"Authorization": user.token,
						'Content-Type': 'application/x-www-form-urlencoded'
					},
					url: url,
					method: "DELETE",
					success: (res) => {
						console.log(res)
					}
				})
			},
			onOps(item, index) {
				let user = uni.getStorageSync("user")
				if (user.power == 3) {
					// uni.showToast({
					// 	title:"权限不足",
					// 	icon:"error"
					// })
					return
				}
				this.item = item
				this.index = index
				// console.log("长按事件")
				this.showOps = !this.showOps
			},
			submittedTab() {
				console.log(this.labelObj)
				console.log(this.labels)
				let flag = true
				if (this.labels.length > 0) {
					for (let index in this.labels) {
						if (this.labelObj.serialNumber == this.labels[index].serialNumber) {
							flag = false
						}
					}
				}
				if (!flag) {
					uni.showToast({
						title: "该编号已存在",
						icon: "error"
					})
				}

				if (this.labelObj.serialNumber == "") {
					uni.showToast({
						title: "请输入编号",
						icon: "error"
					})
					return
				}
				if (this.labelObj.position == "") {
					uni.showToast({
						title: "请输入部位",
						icon: "error"
					})
					return
				}
				if (this.labelObj.inspectionPeriod == "") {
					uni.showToast({
						title: "请输入时段",
						icon: "error"
					})
					return
				}
				if(this.labelObj.address == ""){
					uni.showToast({
						title: "请输入地址",
						icon: "error"
					})
					return
				}

				let user = uni.getStorageSync("user")
				let url = BASE_IP + "pc/tab/save/" + user.unitId
				uni.request({
					header: {
						"Authorization": user.token,
						"Content-Type": "application/json"
					},
					url: url,
					method: "PUT",
					dataType: "json",
					data: {
						"ElectronicTabJson": this.labelObj
					},
					success: (res) => {
						console.log(res)
						if (res.statusCode == 200) {
							if (res.data.code == 1) {
								uni.showToast({
									title: res.data.data,
									icon: "success"
								})
								this.queryTab()
								this.show = !this.show
								this.labelObj.latitude = ""
								this.labelObj.longitude = ""
								this.labelObj.inspectionPeriod = ""
								this.labelObj.address = ""
								this.labelObj.preciseLocation.state = 0
								this.labelObj.preciseLocation.checked = false
								this.labelObj.preciseLocation.text = "关闭"
								this.labelObj.watermarkImgState.state = 0
								this.labelObj.watermarkImgState.checked = false
								this.labelObj.watermarkImgState.text = "关闭"
							} else {
								uni.showToast({
									title: res.data.msg,
									icon: "error"
								})
							}
						} else {
							uni.showToast({
								title: "服务器出现问题",
								icon: "error"
							})
						}
					}
				})
			},
			onWrite() {

				// uni.showModal({
				// 	title: "提示",
				// 	content: "请将手机靠近（NFC）标签开始录入",
				// 	showCancel: true,
				// 	success: (res) => {

				// 		if (res.cancel) {
				// 			this.show = !this.show
				// 		}
				// 		if (res.confirm) {
				// 			if (this.labelObj.serialNumber == "") {
				// 				uni.showToast({
				// 					title: "请输入编号",
				// 					icon: "error"
				// 				})
				// 				return
				// 			}
				// 			if (this.labelObj.position == "") {
				// 				uni.showToast({
				// 					title: "请输入部位",
				// 					icon: "error"
				// 				})
				// 				return
				// 			}
				// 			if (this.labelObj.inspectionPeriod == "") {
				// 				uni.showToast({
				// 					title: "请输入时段",
				// 					icon: "error"
				// 				})
				// 				return
				// 			}

				// 			let las = this.labels
				// 			if (las.length > 0) {
				// 				for (let l in las) {
				// 					if (las[l].serialNumber == this.labelObj.serialNumber) {
				// 						uni.showToast({
				// 							title: "该编号已存在",
				// 							icon: "error"
				// 						})
				// 						return
				// 					} else {
				// 						let temp = {
				// 							id: this.generateRandomNumber(1, 10),
				// 							serialNumber: this.labelObj.serialNumber, //编号
				// 							position: this.labelObj.position, //部位
				// 							inspectionPeriod: this.labelObj.inspectionPeriod, //时段
				// 						}

				// 						this.show = !this.show

				// 						if (uni.getSystemInfoSync().platform == 'android') {
				// 							// listenNFCStatus()
				// 							this.startwrite_ad(temp)
				// 						}
				// 						if (uni.getSystemInfoSync().platform == 'ios') {
				// 							// listenNFCStatus()
				// 							this.startwrite_ios(temp)
				// 						}
				// 						return
				// 					}
				// 				}
				// 			} else {
				// 				let temp = {
				// 					id: this.generateRandomNumber(1, 10),
				// 					serialNumber: this.labelObj.serialNumber, //编号
				// 					position: this.labelObj.position, //部位
				// 					inspectionPeriod: this.labelObj.inspectionPeriod, //时段
				// 				}
				// 				this.show = !this.show
				// 				if (uni.getSystemInfoSync().platform == 'android') {
				// 					// listenNFCStatus()
				// 					this.startwrite_ad(temp)
				// 				}
				// 				if (uni.getSystemInfoSync().platform == 'ios') {
				// 					// listenNFCStatus()
				// 					this.startwrite_ios(temp)
				// 				}

				// 				return
				// 			}
				// 		}
				// 	}
				// })
			},
			startwrite_ad(data) {
				nfc.initNFC()

				setTimeout(() => {
					nfc.writeNFC(JSON.stringify(data), (res) => {
						console.log(res)
						let user = uni.getStorageSync("user")
						if (res == 1) {
							let label = {
								id: data.id,
								serialNumber: data.serialNumber, //编号
								position: data.position, //部位
								inspectionPeriod: data.inspectionPeriod, //时段
								writeTime: "null",
								unitId: user.unitId
							}

							this.labels.push(label)
							this.saveTab(label)
							uni.setStorageSync("labels", this.labels)
						}
					})
				}, 1500)
			},
			startwrite_ios(data) {
				console.log("调用ios")
				this.beginSession(data)
			},
			isSupportsNFCRead() {
				KJNFC.isSupportsNFCRead((res) => {
					console.log("isSupportsNFCRead：" + JSON.stringify(res))
				});
			},
			isSupportsNFCWrite() {
				KJNFC.isSupportsNFCWrite((res) => {
					console.log("isSupportsNFCWrite：" + JSON.stringify(res))
				});
			},
			beginSession(data) {
				/**
				 * ios11-ios12，只有读取，ios13及以上，有读与写
				 * */
				var dic = {
					"alertMessage": "准备扫描，请将标签贴近手机", //提示的信息
					"invalidateAfterFirstRead": false, //是否第一个NDEF标签读取成功后，会话将自动失效
				}
				KJNFC.beginSession(dic, (res) => {
					console.log(JSON.stringify(res));
					if (res.type == "beginSession") {
						console.log("开始：" + JSON.stringify(res))
					} else if (res.type == "readerSessionDidBecomeActive") { //ios13及以上才会回调
						console.log("开始完成：" + JSON.stringify(res))
					} else if (res.type == "didInvalidateWithError") {
						console.log("扫描失败：" + JSON.stringify(res))
						if (res.errorCode == 200) {
							console.log("取消扫描")
						} else if (res.errorCode == 201) {
							console.log("扫描超时")
						}
					} else if (res.type == "didDetectNDEFs") { //ios11-ios12,才会回调
						console.log("检测NDEF：" + JSON.stringify(res))
					} else if (res.type == "didDetectTags") { //从这里开始，某个环节失败，都不会往下执行，ios13及以上才会回调
						console.log("读取标签：" + JSON.stringify(res))
						var tags = res.tags; //返回读取到的所有标签，isAvailable-是否可用
						if (tags.length > 0) {
							var tagIndex = 0; //传tags数组的索引
							KJNFC.connectToTag({
								"tagIndex": tagIndex
							}, (res) => {
								console.log("连接到标签：" + JSON.stringify(res))
								if (res.result == true) {
									KJNFC.queryNDEFStatus({
										"tagIndex": tagIndex
									}, (res) => {
										console.log("查询NDEF状态：" + JSON.stringify(res))
										if (res.result == true) {
											var status = res
												.status; //NDEF状态 1-不允许NDEF读取和写入 2-可读写 3-只读
											if (status == 2 || status == 3) {
												KJNFC.readNDEF({
													"tagIndex": tagIndex
												}, (res) => {
													//res.records
													console.log("读NDEF：" + JSON.stringify(
														res))
												});
											}
											if (status == 2) {
												/**
												 * typeNameFormat: 类型名称 Empty NFCWellKnown Media AbsoluteURI NFCExternal Unknown Unchanged
												 * type: //类型 1代表：http://www. 2代表：https://www. 3代表：http:// 4代表：https:// 5代表：tel: 6代表：mailto: 大于6代表写入ndef内容 
												 * 当类型为U是，payload可以填入-https://www.example.com  -mailto:user@example.com  -tel:+1851000888  -sms:+1851000888   -facetime://:user@example.com
												 * identifier: 唯一id,可以随便设置
												 * payload: 内容 
												 * */
												let user = uni.getStorageSync("user")
												let label = {
													id: data.id,
													serialNumber: data.serialNumber, //编号
													position: data.position, //部位
													inspectionPeriod: data
														.inspectionPeriod, //时段
													writeTime: "null",
													unitId: user.unitId

												}

												var dic = {
													"tagIndex": tagIndex,
													"records": [{
														"typeNameFormat": "NFCWellKnown",
														"type": "U",
														"identifier": label.id,
														"payload": JSON.stringify(
															label)
													}]
												}
												KJNFC.writeNDEF(dic, (res) => {


													if (res.result) {
														console.log("写入成功")
														this.invalidateSession()
														let user = uni.getStorageSync(
															"user")
														let label = {
															id: data.id,
															serialNumber: data
																.serialNumber, //编号
															position: data
																.position, //部位
															inspectionPeriod: data
																.inspectionPeriod, //时段
															writeTime: "null",
															unitId: user.unitId
														}
														console.log("写NDEF：" + JSON
															.stringify(
																res))

														this.labels.push(label)
														this.saveTab(label)
														uni.setStorageSync("labels", this
															.labels)
													}
												});
											}
										}
									})
								}
							})
						}
					}
				});
			},
			restartPolling() {
				KJNFC.restartPolling((res) => { //ios13及以上支持
					console.log("restartPolling：" + JSON.stringify(res))
				});
			},
			invalidateSession() {
				KJNFC.invalidateSession((res) => {
					console.log("invalidateSession：" + JSON.stringify(res))
				});
			},
			setAlertMessage() {
				KJNFC.setAlertMessage({
					"alertMessage": ""
				}, (res) => {
					console.log("setAlertMessage：" + JSON.stringify(res))
				});
			},
			saveTab(data) {
				let user = uni.getStorageSync("user")
				let url = BASE_IP + "pc/tab/save/" + data.id + "/" + user.unitId + "/" + data.serialNumber + "/" + data
					.position + "/" + data.inspectionPeriod + "/" + data.writeTime
				uni.request({
					header: {
						"Authorization": user.token,
						'Content-Type': 'application/x-www-form-urlencoded'
					},
					url: url,
					method: "PUT",
					success: (res) => {
						console.log(res)

					}
				})
			},
			generateRandomNumber(minLength, maxLength) {
				// 生成随机数
				let randomNumber = Math.random();

				// 计算随机数的位数
				let length = Math.floor(Math.random() * (maxLength - minLength + 1)) + minLength;

				// 将随机数转换为字符串
				let randomNumberString = randomNumber.toString().substr(2, length);

				return randomNumberString;
			},
			oOpsNum(res) {
				this.showNum = !this.showNum
				this.labelObj.serialNumber = res.value[0]
			},
			oOpsTime(res) {
				this.showTime = !this.showTime
				this.labelObj.inspectionPeriod = res.value[0]
			},
			onVibrate() {
				let platform = uni.getSystemInfoSync().platform
				// #ifdef APP-PLUS
				if (platform == "ios") {
					let UIImpactFeedbackGenerator = plus.ios.importClass('UIImpactFeedbackGenerator');
					let impact = new UIImpactFeedbackGenerator();
					impact.prepare();
					impact.init(1);
					impact.impactOccurred();
				}
				if (platform == "android") {
					uni.vibrateShort();
				}
			},
			queryTab() {
				let user = uni.getStorageSync("user")
				let url = BASE_IP + "pc/tab/query/" + user.unitId
				uni.request({
					header: {
						"Authorization": user.token,
						'Content-Type': 'application/x-www-form-urlencoded'
					},
					url: url,
					method: "GET",
					success: (res) => {
						console.log(res)
						if (res.cookies == 500) {
							uni.showToast({
								title: "服务器出现问题",
								icon: "error"
							})
							return
						}
						if (res.cookies == 404) {
							uni.showToast({
								title: "获取数据失败",
								icon: "error"
							})
							return
						}
						if (res.statusCode == 200) {
							let ls = res.data
							this.labels = ls
						}
					},
					fail: () => {
						uni.showToast({
							title: "服务器出现问题",
							icon: "error"
						})
						return
					}
				})
			}
		},
		onLoad() {
			// console.log("11111111111")
			setTimeout(() => {
				this.queryTab()
			}, 500)
			// setTimeout(() => {
			// 	this.queryTab()
			// }, 800)
			const currentDate = moment();
			const year = currentDate.year();
			const month = currentDate.month() + 1;
			const day = currentDate.date();
			this.time = year + "-" + month + "-" + day
			console.log(this.time)
			let record = uni.getStorageSync("record")
			for (let r in record) {
				if (record[r].classifyId == 1) {
					let recorditme = JSON.stringify(record[r])
					this.recorditme = recorditme
					// console.log(recorditme)
				}
			}
			// console.log(record)

		},
		onShow() {
			setTimeout(() => {
				this.queryTab()
			}, 500)
			// setTimeout(() => {
			// 	this.queryTab()
			// }, 800)
			const currentDate = moment();
			const year = currentDate.year();
			const month = currentDate.month() + 1;
			const day = currentDate.date();
			this.time = year + "-" + month + "-" + day
			console.log(this.time)
			let record = uni.getStorageSync("record")
			for (let r in record) {
				if (record[r].classifyId == 1) {
					let recorditme = JSON.stringify(record[r])
					this.recorditme = recorditme
					// console.log(recorditme)
				}
			}

			// setTimeout(() => {
			// 	this.queryTab()
			// }, 500)
			// setTimeout(() => {
			// 	this.queryTab()
			// }, 800)

			// let ls = uni.getStorageSync("labels")
			// if (ls != "" && ls != null && ls != undefined) {
			// 	this.labels = ls
			// 	// console.log(ls)
			// }

			// const currentDate = moment();
			// const year = currentDate.year();
			// const month = currentDate.month() + 1;
			// const day = currentDate.date();
			// this.time = year + "-" + month + "-" + day


			// let record = uni.getStorageSync("record")
			// for (let r in record) {
			// 	if (record[r].classifyId == 1) {
			// 		let recorditme = JSON.stringify(record[r])
			// 		this.recorditme = recorditme
			// 		console.log(recorditme)
			// 	}
			// }
		},
		onNavigationBarButtonTap(res) {
			 console.log("tab：",res)
			 this.show =!this.show
		},
		onPullDownRefresh(){
			this.queryTab()
			uni.stopPullDownRefresh();
		}
	}
</script>

<style>
	page {
		background-color: #FAFCFF;
	}

	.fui-card__content {
		display: flex;
		justify-content: space-between;
		align-items: center;
		height: 80rpx;
		width: 90%;
		margin: 0 auto;
	}
</style>